﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SubstringExtension
{
    public static class ExtensionMethod
    {
        public static StringBuilder Substring(this StringBuilder str, int index)
        {
            if (index < 0 || index >= str.Length)
            {
                throw new ArgumentOutOfRangeException();
            }
            string strAsString = str.ToString();
            string substring = strAsString.Substring(index);
            StringBuilder result = new StringBuilder(substring);
            return result;
        }

        public static StringBuilder Substring(this StringBuilder str, int index, int length)
        {
            if (index < 0 || length < 0 || index + length > str.Length)
            {
                throw new ArgumentOutOfRangeException();
            }
            string strAsString = str.ToString();
            string substring = strAsString.Substring(index, length);
            StringBuilder result = new StringBuilder(substring);
            return result;
        }
    }

    class SubstringExtension
    {
        static void Main()
        {
            StringBuilder testStringBuilder = new StringBuilder("Hello C Sharp!");
            
            StringBuilder testSubstringOneArgument = testStringBuilder.Substring(6);
            Console.WriteLine(testSubstringOneArgument.ToString());

            StringBuilder testSubstringTwoArguments = testStringBuilder.Substring(6, 5);
            Console.WriteLine(testSubstringTwoArguments.ToString());
        }
    }
}
